/**
 * 
 */
package com.ap.core.transaction;

import java.math.BigInteger;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;

/**
 * The Helper Class for Encoding the AP Transaction Id.
 *
 * @author Dilip.Sridhar
 */
public class APTransactionGeneratorUtil {
	
	/** The Constant DECIMAL. */
	public static final int DECIMAL = 10;
	
	/** The Constant HEXA_DECIMAL. */
	public static final int HEXA_DECIMAL = 16;
	
	/** The Constant BASE36. */
	public static final int BASE36 = Character.MAX_RADIX;
	

	/**
	 * Generate the AP Transaction id.
	 *
	 * @param apTxnRetrievalId the ap txn retrieval id
	 * @return the string
	 */
	public static String generateBase36EncodedAPTransactionId(String apTxnRetrievalId) {
        final String firstPart = encodeToBase36(apTxnRetrievalId,DECIMAL,12);
        
        final String secondPart = encodeToBase36(getUUIDGenerator(), HEXA_DECIMAL, 25);
       
		return (firstPart + secondPart);
	}
	
	private static String encodeToBase36(String inputString, int toFormat, int maxLimit) {
        final BigInteger bigInt = new BigInteger(inputString, toFormat);
        return StringUtils.leftPad(bigInt.toString(BASE36), maxLimit, "0");
	}
	
	private static String  getUUIDGenerator(){
		return UUID.randomUUID().toString().replaceAll("-","");
	}

}
